package toanstt.com;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Random;

import toanstt.gameplay.GameManagement;
import toanstt.mainframe.MainFrame;
import toanstt.shop.Shop;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.os.Debug;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


public class App extends SurfaceView implements Runnable
{
	//BitmapFont b1;
	
	public static long currenttime;
	long lasttime;
	Thread t=null;
	SurfaceHolder holder;
	boolean isItOK=false;
	static int iscreen = 0;
	// CellTable celltable;
	static  public GameManagement gamemanagement;
	static   public MainFrame mainframe;
	Loading loading;
	static   public Shop shop;
	//sprite = new Sprite(this,blob);
	boolean gamegloaded = false;
	CallBackFunction callbackfunction;
	public App(Context context) 
	{
		super(context);
		holder = getHolder();
		
		loading = new Loading();
		callbackfunction = new CallBackFunction();
		T.SCREEN = SCR.MENU;
		T.loadGame();
		//gamemanagement = new GameManagement();
	}
	public void run() 
	{
		
		while(isItOK == true)
		{
			
			if(gamegloaded == false)
			{
				


				
				currenttime = System.currentTimeMillis();
				if(currenttime- lasttime < 50)
					Thread.yield();
				else
				{
					lasttime = currenttime;
				
					if(!holder.getSurface().isValid())
						continue;
					Canvas c = holder.lockCanvas();
					loading.Draw(c);
					holder.unlockCanvasAndPost(c);
					T.Init(DiamondLinkinActivity.activity);
					gamemanagement = new GameManagement();
					mainframe = new MainFrame();
					shop = new Shop();
					T.SCREEN =1;
					gamegloaded = true;
				
				}
			}
			else
			{
			currenttime = System.currentTimeMillis();
			if(currenttime- lasttime < 50)
				Thread.yield();
			else{lasttime = currenttime;
			
			if(!holder.getSurface().isValid())
				continue;
			
			callbackfunction.Update();
			Canvas c = holder.lockCanvas();
			
			
				//T.notice.Draw(c);
			//	doalpha = true;
				c.drawARGB(100, 0, 0, 0);
			
				if(T.onnotice==false)
				{
					switch(T.SCREEN)
					{
					case 1:
						mainframe.Update();
						break;
					case 2:
						gamemanagement.Update();
						break;
					case 3:
						break;
					}
				}
				
			
			mainDraw(c);
			holder.unlockCanvasAndPost(c);
			}
			
		}}
	}
	protected void mainDraw(Canvas canvas)
	{
		
		//
		canvas.drawBitmap(T.bitmap_BackGround1,  0,0,null);//
		canvas.drawARGB(150, 0, 0,0);
		//DrawMainFram(canvas);
		//gamemanagement.Draw(canvas);	
		switch(T.SCREEN)
		{
		case 1:
			mainframe.Draw(canvas);
			break;
		case 2:
			gamemanagement.Draw(canvas);	
			break;
		case 3:
			shop.Draw(canvas);
			break;
		}
		if(T.onnotice==true)
		{
			T.notice.Draw(canvas);
		}
	}
	
	
	public void pause()
	{
		isItOK = false;
		while(true)
		{
			try
			{
				t.join();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			break;
		}
		t=null;
	}
	public void resume()
	{
		isItOK = true;
		t = new Thread(this);
		t.start();
	}
	public void touch(MotionEvent mo)
	{
		if(gamegloaded == true)
		{
			if(T.onnotice==true)
				T.notice.toch(mo);
			else
			switch(T.SCREEN)
			{
			case 1:
				mainframe.touch(mo);
				break;
			case 2:
				gamemanagement.touch(mo);
				break;
			case 3:
				shop.touch(mo);
				break;
			}
		}

	}
}